import { loadPoint } from '@/hooks/earth/utils'
import { v1 } from 'uuid'

import _ from 'lodash'

class Target {
  constructor() {
    this.viewer = window.viewer
    // 添加相关图层
    this._layer = new Cesium.CustomDataSource('targetlayer')
    this.viewer.dataSources.add(this._layer)
  }

  removeAll() {
    // 删除3dtiles
    if (_.has(this, '_tileset')) {
      viewer.scene.primitives.remove(this._tileset)
      delete this['_tileset']
    }
    // 删除图层
    this._layer.entities.removeAll()
    // remove
    //删除影像模型
    if (_.has(this, '_imageLayer')) {
      viewer.imageryLayers.remove(this._imageLayer)
      delete this['_imageLayer']
    }
  }

  load(item) {
    this.removeAll()
    // 模型 3dtiles 影像
    /**
 
    {lng: 112.82952746128707, lat: 34.79838865917642, alt: 0},
    {lng: 114.29981743816764, lat: 34.79838865917643, alt: 0},
    {lng: 114.29981743816762, lat: 33.98199126111562, alt: 0},
    {lng: 112.82952746128707, lat: 33.98199126111562, alt: 0}
    
    * 
    */
    switch (item.type) {
      case 'obj':

        this.loadObj(item)
        break
      case 'tif':
        this.loadImage(item)
        break
      default:
        break
    }
  }

  loadObj(item) {
    if (item.relpath.includes('.json')) {
      this.loadTileSet(item)
    }
    if (item.relpath.includes('.gl')) {
      this.loadModel(item)
    }
  }

  loadModel(item) {
    let e = this._layer.entities.add({
      name: item.relpath,
      // 和时间轴关联
      show: true,
      position: Cesium.Cartesian3.fromDegrees(
        item?.position
      ),
      // 模型数据
      model: {
        id: v1(),
        name: item.relpath,
        minimumPixelSize: 128,
        maximumScale: 200000,
        uri: `/api/download/${item.relpath}`,
        scale: 1,
      },
    })
    viewer.flyTo(e, {
      offset: new Cesium.HeadingPitchRange(0, -1.7, 2000),
      duration: 1
    })
  }

  loadImage(item) {

    let range = [{ lng: 112.82952746128707, lat: 34.79838865917642, alt: 0 },
    { lng: 114.29981743816764, lat: 34.79838865917643, alt: 0 },
    { lng: 114.29981743816762, lat: 33.98199126111562, alt: 0 },
    { lng: 112.82952746128707, lat: 33.98199126111562, alt: 0 }]

    //两种方式1.直接用cesium 2.使用earthsdk 卷帘需要用

    let basemapSource = new Cesium.WebMapServiceImageryProvider({
      // 这里是你的 geoserver服务点击查看图层的 url
      url: '/map_api/wms?file=gf1.tif&FORMAT=image/png',
      // 这里是自定义的图层名称
      parameters: {
        service: 'WMS'
      }
    });
    this._imageLayer = viewer.imageryLayers.addImageryProvider(basemapSource);


    // TODO: 添加 wms图层
    // var rect;
    // this._imageLayer = viewer.imageryLayers.addImageryProvider(new Cesium.UrlTemplateImageryProvider({
    //   url: item.extraInfo.data.meta.url,
    //   rectangle: rect,
    //   //minimumLevel:0,
    //   //maximumLevel:17
    // }));

    // 添加影像范围
    let polgonPositions = []
    item.details.geometry.coordinates.map(positem => {
      polgonPositions.push(Cesium.Cartesian3.fromDegrees(positem[0], positem[1]))
    })


    this._layer.entities.add({
      name: "多边形",
      id: v1(),
      polygon: {
        show: true,
        hierarchy: new Cesium.PolygonHierarchy(polgonPositions),
        // height: 0,
        material: Cesium.Color.CYAN.withAlpha(0.0),
        outline: true,
        outlineColor: Cesium.Color.BLACK,
        classificationType: Cesium.ClassificationType.BOTH
      },
      polyline: {
        positions: [...polgonPositions],
        width: 3,
        material: Cesium.Color.BLUE.withAlpha(0.5),
        clampToGround: true,
      },
    })

    //飞到影像中心点
    viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(
        item.details.geometry.center_coording[0] || 0,
        item.details.geometry.center_coording[1] || 0,
          100000,
      ),
      duration: 0.8,
    })


  }

  loadTileSet(item) {

    var tileset = viewer.scene.primitives.add(
      new Cesium.Cesium3DTileset({
        url: `/api/download/${item.relpath.replace('\\', '/')}`,
        // url: item.extraInfo.data.meta.url,
        // modelMatrix: Cesium.Matrix4.fromArray(
        //   item.extraInfo.data.meta.modelMatrix,
        // ),
      }),
    )
    viewer.flyTo(tileset)
    this._tileset = tileset
  }

  loadPort(item) {
    var tileset = viewer.scene.primitives.add(
      new Cesium.Cesium3DTileset({
        url: item.extraInfo.data.meta.url,
        modelMatrix: Cesium.Matrix4.fromArray(
          item.extraInfo.data.meta.modelMatrix,
        ),
      }),
    )
    viewer.flyTo(tileset)
    this._tileset = tileset
  }
}

// export {move}
export default Target
